package com.formina.stars;

import java.util.Random;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLES20;
import android.opengl.Matrix;

import com.formina.engine.Engine;
import com.formina.engine.Model;

public class StarParticle extends Model
{
	
	int rotationAxis;
	public StarParticle(float[] location, float[] scale){
 	   super(location, scale,Engine.StarParticle);
 	   Random r = new Random();
	   rotationAxis = r.nextInt(3);
	   this.speed = 0;
	   while(speed==0){
		   this.speed=(float)r.nextInt(5);
	   }
    }
	   
	   public StarParticle() {
		  this(new float[]{0,0,0}, new float[]{1f,1f,1f});
	   }
	   
	   @Override
	   public void update(long time){
		   setX(getX()+getVelocity()[0]*getSpeed());
		   setY(getY()+getVelocity()[1]* getSpeed());
		   setZ(getZ()+getVelocity()[2]* getSpeed());
		   setAlpha(Color()[3]-.02f);;
		   Rotation()[rotationAxis] += 4f;
		   if (Rotation()[rotationAxis]>359){
			   Rotation()[rotationAxis]=0;
		   }
		   if(this.Color()[3] <=0){
			   this.isDead = true;
		   }
	   }
	   
	   @Override
	    public void draw(GL10 gl) {
		// Use culling to remove back faces.
	        GLES20.glEnable(GLES20.GL_CULL_FACE);
	        GLES20.glEnable(GLES20.GL_BLEND);
	        GLES20.glBlendFunc (GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);	         
	        // Enable depth testing
	        GLES20.glEnable(GLES20.GL_DEPTH_TEST);
	        
	        Matrix.setIdentityM(mModelMatrix, 0);
	        Matrix.setIdentityM(rotationMatrix, 0);
	        Matrix.setIdentityM(scaleMatrix, 0);
	        Matrix.translateM(mModelMatrix, 0, this.getX(), this.getY(), this.getZ());
	        Matrix.scaleM(mModelMatrix, 0, Scale()[0], Scale()[1],Scale()[2]);
	        Matrix.rotateM(mModelMatrix, 0, this.Rotation()[this.rotationAxis], 0f, 0f, 1f);

	        
	        GLES20.glVertexAttrib3fv(Engine.mLightPositionHandle, new float[] {getX(), getY(), getZ()+1000}, 0);
	        
	        //position
	        modelInfo.vertexBuffer.position(0);
	        GLES20.glVertexAttribPointer(Engine.mPositionHandle, 3, GLES20.GL_FLOAT, false,
	        		0, modelInfo.vertexBuffer);
			GLES20.glEnableVertexAttribArray(Engine.mPositionHandle);
	        //end position
			
			//color
	        GLES20.glVertexAttrib4f(Engine.mColorHandle, this.Color()[0],this.Color()[1],this.Color()[2],this.Color()[3]);

			//GLES20.glVertexAttribPointer(Engine.mColorHandle, 3, GLES20.GL_FLOAT, false,
	        		//0, this.ColorBufferV());    
	        //GLES20.glEnableVertexAttribArray(Engine.mColorHandle);
			//end color
	        
	        //normals for lighting
	        GLES20.glVertexAttribPointer(Engine.mNormalHandle, 3, GLES20.GL_FLOAT, false, 
	        		0, this.modelInfo.normalBuffer);
	        GLES20.glEnableVertexAttribArray(Engine.mNormalHandle);

	        //end normals
				       	        
	        Matrix.multiplyMM(Engine.mMVPMatrix, 0, Engine.mVMatrix, 0, mModelMatrix, 0);
	        GLES20.glUniformMatrix4fv(Engine.mMVMatrixHandle, 1, false, Engine.mMVPMatrix, 0);

	        Matrix.multiplyMM(Engine.mMVPMatrix, 0, Engine.mProjMatrix, 0, Engine.mMVPMatrix, 0);
	        GLES20.glUniformMatrix4fv(Engine.mMVPMatrixHandle, 1, false, Engine.mMVPMatrix, 0);
	        
	        // Pass in the light position in eye space.        
	        GLES20.glUniform3f(Engine.mLightPosHandle, Engine.mLightPosInEyeSpace[0], Engine.mLightPosInEyeSpace[1], Engine.mLightPosInEyeSpace[2]);
	        
	        //color
	        //GLES20.glUniform4fv(Engine.mColorHandle, 1, this.ColorBuffer());
	        //end color
	        
	        GLES20.glDrawElements(GLES20.GL_TRIANGLES, modelInfo.Indices.length,// OpenGL docs
					GLES20.GL_UNSIGNED_SHORT, modelInfo.indexBuffer);
	        
	        GLES20.glDisable(GLES20.GL_ALPHA);
	        GLES20.glDisable(GLES20.GL_CULL_FACE);
	        GLES20.glDisable(GLES20.GL_DEPTH_TEST);
	   }
}
